import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:my_flutter_github/model/User.dart';
import 'package:my_flutter_github/model/UserOrg.dart';

import 'package:my_flutter_github/MyStyle.dart';
import 'package:my_flutter_github/utils/CommonUtils.dart';
import 'package:my_flutter_github/utils/NavigatorUtils.dart';
import 'package:my_flutter_github/widgets/CardItem.dart';
import 'package:my_flutter_github/widgets/IconTextWidget.dart';
import 'package:my_flutter_github/widgets/UserIconWidget.dart';

import 'package:flutter_svg/flutter_svg.dart';

///用户详情头部
class UserHeaderItemWidget extends StatelessWidget {
  final User userInfo;

  final String beStaredCount;

  final Color notifyColor;

  final Color themeColor;

  final VoidCallback refreshCallBack;

  final List<UserOrg> orgList;

  const UserHeaderItemWidget(this.userInfo, this.beStaredCount, this.themeColor,
      {this.notifyColor, this.refreshCallBack, this.orgList});

  ///底部状态栏
  _getBottomItem(String title, var value, onPressed) {
    String data = value == null ? "" : value.toString();
    TextStyle titleStyle = (title != null && title.toString().length > 6)
        ? MyConstants.minText
        : MyConstants.smallSubLightText;
    TextStyle valueStyle = (value != null && value.toString().length > 6)
        ? MyConstants.minText
        : MyConstants.smallSubLightText;
    return new Expanded(
      child: new Center(
          child: new RawMaterialButton(
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              padding: EdgeInsets.only(top: 5.0),
              constraints: const BoxConstraints(minWidth: 0.0, minHeight: 0.0),
              child: RichText(
                textAlign: TextAlign.center,
                text: TextSpan(
                  children: [
                    TextSpan(text: title, style: titleStyle),
                    TextSpan(text: "\n", style: valueStyle),
                    TextSpan(text: data, style: valueStyle)
                  ],
                ),
              ),
              onPressed: onPressed)),
    );
  }

  ///我的界面--通知Icon（可点击跳转到通知界面）
  _getNotifyIcon(BuildContext context, Color color) {
    if (notifyColor == null) {
      return Container();
    }
    return new RawMaterialButton(
        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
        padding: const EdgeInsets.only(top: 0.0, right: 5.0, left: 5.0),
        constraints: const BoxConstraints(minWidth: 0.0, minHeight: 0.0),
        child: new ClipOval(
          child: new Icon(
            MyIcons.USER_NOTIFY,
            color: color,
            size: 18.0,
          ),
        ),
        onPressed: () {
          NavigatorUtils.goNotifyPage(context).then((res) {
            refreshCallBack?.call();
          });
        });
  }

  ///用户组织
  _renderOrgs(BuildContext context, List<UserOrg> orgList) {
    print("_renderOrgs.length:${orgList.length}");
    if (orgList == null || orgList.length == 0) {
      return new Container();
    }
    List<Widget> list = new List();

    renderOrgsItem(UserOrg orgs) {
      return UserIconWidget(
          padding: const EdgeInsets.only(right: 5.0, left: 5.0),
          width: 30.0,
          height: 30.0,
          image: orgs.avatarUrl ?? MyIcons.DEFAULT_REMOTE_PIC,
          onPressed: () {
            NavigatorUtils.goPerson(context, orgs.login);
          });
    }

    int length = orgList.length > 3 ? 3 : orgList.length;

    list.add(new Text("所在组织" + ":", style: MyConstants.smallSubLightText));

    for (int i = 0; i < length; i++) {
      list.add(renderOrgsItem(orgList[i]));
    }
    if (orgList.length > 3) {
      list.add(new RawMaterialButton(
          onPressed: () {
            NavigatorUtils.gotoCommonList(
                context, userInfo.login + " " + "所在组织", "org", "user_orgs",
                userName: userInfo.login);
          },
          materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
          padding: const EdgeInsets.only(right: 5.0, left: 5.0),
          constraints: const BoxConstraints(minWidth: 0.0, minHeight: 0.0),
          child: Icon(
            Icons.more_horiz,
            color: Color(MyColors.white),
            size: 18.0,
          )));
    }
    return Row(children: list);
  }

  _renderChart(context) {
    double height = 140.0;
    double width = 3 * MediaQuery.of(context).size.width / 2;
    if (userInfo.login != null && userInfo.type == "Organization") {
      return new Container();
    }
    return (userInfo.login != null)
        ? new Card(
            margin: EdgeInsets.only(
                top: 0.0, left: 10.0, right: 10.0, bottom: 10.0),
            color: Color(MyColors.white),
            child: new SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: new Container(
                padding: EdgeInsets.only(left: 10.0, right: 10.0),
                width: width,
                height: height,

                ///svg chart
                child: new SvgPicture.network(
                  CommonUtils.getUserChartAddress(userInfo.login),
                  width: width,
                  height: height - 10,
                  allowDrawingOutsideViewBox: true,
                  placeholderBuilder: (BuildContext context) => new Container(
                        height: height,
                        width: width,
                        child: Center(
                          child: SpinKitRipple(
                              color: Theme.of(context).primaryColor),
                        ),
                      ),
                ),
              ),
            ),
          )
        : new Container(
            height: height,
            child: Center(
              child: SpinKitRipple(color: Theme.of(context).primaryColor),
            ),
          );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        CardItem(
          color: themeColor,
          margin: EdgeInsets.all(10.0),
//          shape: new RoundedRectangleBorder(borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10.0), bottomRight: Radius.circular(10.0))),
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[

                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    ///用户头像
                    RawMaterialButton(
                      onPressed: () {
                        if (userInfo.avatar_url != null) {
                          NavigatorUtils.gotoPhotoViewPage(
                              context, userInfo.avatar_url);
                        }
                      },
                      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                      padding: EdgeInsets.all(0.0),
                      constraints:
                          BoxConstraints(minWidth: 0.0, minHeight: 0.0),
                      child: ClipOval(
                        child: FadeInImage.assetNetwork(
                          placeholder: MyIcons.DEFAULT_USER_ICON,
                          fit: BoxFit.fitWidth,
                          image:
                              userInfo.avatar_url ?? MyIcons.DEFAULT_USER_ICON,
                          width: 80.0,
                          height: 80.0,
                        ),
                      ),
                    ),
                    Padding(padding: EdgeInsets.all(10.0)),
                    Expanded(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            ///用户名
                            Text(
                              userInfo.login ?? "",
                              style: MyConstants.largeTextWhiteBold,
                            ),

                            ///通知图标
                            _getNotifyIcon(context, notifyColor),
                          ],
                        ),

                        ///用户名显示
                        Text(
                          userInfo.name ?? "",
                          style: MyConstants.smallSubLightText,
                        ),

                        ///用户组织（公司）
                        IconTextWidget(
                          iconData: MyIcons.USER_ITEM_COMPANY,
                          iconText: userInfo.company ?? "该用户没有设置",
                          textStyle: MyConstants.smallSubLightText,
                          iconColor: Color(MyColors.subLightTextColor),
                          iconSize: 10.0,
                          padding: 3.0,
                        ),

                        ///用户位置（公司）
                        IconTextWidget(
                          iconData: MyIcons.USER_ITEM_LOCATION,
                          iconText: userInfo.location ?? "该用户没有设置",
                          textStyle: MyConstants.smallSubLightText,
                          iconColor: Color(MyColors.subLightTextColor),
                          iconSize: 10.0,
                          padding: 3.0,
                        ),
                      ],
                    )),
                  ],
                ),

                //<===== 头像用户信息结束

                /// blog 标签
                Container(
                  alignment: Alignment.topLeft,
                  margin: EdgeInsets.all(10.0),
                  child: RawMaterialButton(
                    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                    padding: const EdgeInsets.all(0.0),
                    constraints: const BoxConstraints(minWidth: 0.0, minHeight: 0.0),
                    child: IconTextWidget(
                      iconData: MyIcons.USER_ITEM_LINK,
                      iconText: userInfo.blog ?? "该用户暂未设置博客",
                      textStyle: (userInfo.blog == null) ? MyConstants.smallSubLightText : MyConstants.smallActionLightText,
                      iconColor: Color(MyColors.subLightTextColor),
                      iconSize: 10.0,
                      padding: 3.0,
                    ),
                    onPressed: () {
                      if (userInfo.blog != null) {
                        CommonUtils.launchOutURL(context, userInfo.blog);
                      }
                    },
                  ),
                ),

                ///组织
                _renderOrgs(context, orgList),

                ///用户描述
                new Container(
                    child: new Text(
                      userInfo.bio == null
                          ? "创建于: " + CommonUtils.getDateStr(userInfo.created_at)
                          : userInfo.bio + "\n" + "创建于: " + CommonUtils.getDateStr(userInfo.created_at),
                      style: MyConstants.smallSubLightText,
                    ),
                    margin: new EdgeInsets.only(top: 0.0, bottom: 2.0),
                    alignment: Alignment.topLeft),
                new Padding(padding: EdgeInsets.only(bottom: 4.0)),
                new Divider(
                  color: Color(MyColors.subLightTextColor),
                ),

                ///用户底部状态
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    _getBottomItem("仓库", userInfo.public_repos, (){
                      NavigatorUtils.gotoCommonList(context, userInfo.login, "repository", "user_repos", userName: userInfo.login);
                    }),
                    new Container(width: 0.3, height: 40.0, color: Color(MyColors.subLightTextColor)),
                    _getBottomItem(
                      "粉丝",userInfo.followers, () {
                        NavigatorUtils.gotoCommonList(context, userInfo.login, "user", "follower", userName: userInfo.login);
                      },
                    ),
                    new Container(width: 0.3, height: 40.0, color: Color(MyColors.subLightTextColor)),
                    _getBottomItem("关注", userInfo.following,() {
                        NavigatorUtils.gotoCommonList(context, userInfo.login, "user", "followed", userName: userInfo.login);
                      },
                    ),
                    new Container(width: 0.3, height: 40.0, color: Color(MyColors.subLightTextColor)),
                    _getBottomItem("星标", userInfo.starred, () {
                        NavigatorUtils.gotoCommonList(context, userInfo.login, "repository", "user_star", userName: userInfo.login);
                      },
                    ),
                    new Container(width: 0.3, height: 40.0, color: Color(MyColors.subLightTextColor)),
                    _getBottomItem("荣耀", beStaredCount, () {},),

                  ],
                )
              ],
            ),
          ),
        ),
        new Container(
            child: new Text(
              (userInfo.type == "Organization")
                  ? "组织动态"
                  : "个人动态",
              style: MyConstants.normalTextBold,
              overflow: TextOverflow.ellipsis,
            ),
            margin: new EdgeInsets.only(top: 15.0, bottom: 15.0, left: 12.0),
            alignment: Alignment.topLeft),
        _renderChart(context),
      ],
    );
  }
}
